package com.gentlemancredit.app.utils;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.text.Html;
import android.text.TextUtils;


/**
 * @author YCJ
 * @创建时间 2017/11/19 22:51
 * @说明
 */
public class DialogHelp {

    /***
     * 获取一个dialog
     * @param context
     * @return
     */
    private static AlertDialog.Builder getDialog(Context context) {
        return new AlertDialog.Builder(context);
    }

    /***
     * 获取一个耗时等待对话框
     * @param context
     * @param message
     * @return
     */
    public static ProgressDialog getWaitDialog(Context context, String message) {
        ProgressDialog waitDialog = new ProgressDialog(context);
        if (!TextUtils.isEmpty(message)) {
            waitDialog.setMessage(message);
        }
        return waitDialog;
    }

    /***
     * 获取一个耗时等待对话框
     * @param context
     * @param message
     * @return
     */
    public static ProgressDialog getWaitDialogNotClose(Context context, String message) {
        ProgressDialog waitDialog = new ProgressDialog(context);
        if (!TextUtils.isEmpty(message)) {
            waitDialog.setMessage(message);
            waitDialog.setCanceledOnTouchOutside(false);
            waitDialog.setCancelable(false);
        }
        return waitDialog;
    }


    /***
     * 获取一个信息对话框，注意需要自己手动调用show方法显示
     * @param context
     * @param message
     * @param onClickListener
     * @return
     */
    private static AlertDialog.Builder getMessageDialog(Context context, String message,
                                                        DialogInterface.OnClickListener onClickListener) {
        AlertDialog.Builder builder = getDialog(context);
        builder.setMessage(message);
        builder.setPositiveButton("确定", onClickListener);
        return builder;
    }

    public static AlertDialog.Builder getMessageDialog(Context context, String message) {
        return getMessageDialog(context, message, null);
    }

    /**
     * 只带确定按钮的温馨提示框。
     *
     * @param context
     * @param message
     * @param onClickListener
     * @return
     */
    public static AlertDialog.Builder getMessageDialogWithTitle(Context context, String message,
                                                                DialogInterface.OnClickListener onClickListener) {
        AlertDialog.Builder builder = getDialog(context);
        builder.setMessage(message);
        builder.setTitle("温馨提示");
        builder.setPositiveButton("确定", onClickListener);
        return builder;
    }

    /**
     * 带【立即前往】按钮的温馨提示框。
     *
     * @param context
     * @param message
     * @param onClickListener
     * @return
     */
    public static AlertDialog.Builder getGrantMessageDialogWithTitle(Context context, String message,
                                                                     DialogInterface.OnClickListener onClickListener) {
        AlertDialog.Builder builder = getDialog(context);
        builder.setMessage(message);
        builder.setTitle("温馨提示");
        builder.setPositiveButton("立即前往", onClickListener);
        return builder;
    }

    /**
     * 权限申请 温馨提示框。
     *
     * @param context
     * @param message
     * @param onClickListener
     * @return
     */
    public static AlertDialog.Builder getMessageDialogWithTitlePermission(Context context, String message,
                                                                          DialogInterface.OnClickListener onClickListener) {
        AlertDialog.Builder builder = getDialog(context);
        builder.setMessage(message);
        builder.setTitle("温馨提示");
        builder.setPositiveButton("前往开启", onClickListener);
        return builder;
    }

    /**
     * 带标题的 对话框
     *
     * @param context
     * @param message
     * @param positiveClick
     * @param negativeClick
     * @return
     */
    public static AlertDialog.Builder getMessageDialogWithTitle(Context context, String message,
                                                                DialogInterface.OnClickListener positiveClick,
                                                                DialogInterface.OnClickListener negativeClick) {
        AlertDialog.Builder builder = getDialog(context);
        builder.setMessage(message);
        builder.setTitle("提示");

        builder.setPositiveButton("确定", positiveClick);
        builder.setNegativeButton("取消", negativeClick);
        return builder;
    }

    //更新提示对话框（强制更新）
    public static AlertDialog.Builder getMustMessageDialogVersion(Context context, String message,
                                                                  DialogInterface.OnClickListener positiveClick,
                                                                  DialogInterface.OnClickListener negativeClick) {
        AlertDialog.Builder builder = getDialog(context);
        String lable = "您需要更新应用才能继续使用\n\n";
        builder.setMessage(lable + message);
        builder.setTitle("应用更新");
        builder.setPositiveButton("立即更新", positiveClick);
        builder.setNegativeButton("退出应用", negativeClick);
        return builder;
    }

    //更新提示对话框（可忽略当前版本）
    public static AlertDialog.Builder getMessageDialogVersion(Context context, String message,
                                                              DialogInterface.OnClickListener positiveClick,
                                                              DialogInterface.OnClickListener negativeClick) {
        AlertDialog.Builder builder = getDialog(context);
        builder.setMessage(message);
        builder.setTitle("应用更新");
        builder.setPositiveButton("立即更新", positiveClick);
        builder.setNegativeButton("以后再说", negativeClick);
        return builder;
    }


    public static AlertDialog.Builder getOpenPermissionDialog(Context context, String message,
                                                              DialogInterface.OnClickListener positiveClick,
                                                              DialogInterface.OnClickListener negativeClick) {
        AlertDialog.Builder builder = getDialog(context);
        builder.setMessage(message);
        builder.setTitle("温馨提示");
        builder.setPositiveButton("去开启", positiveClick);
        builder.setNegativeButton("取消", negativeClick);
        return builder;
    }


    public static AlertDialog.Builder getConfirmDialog(Context context, String message, DialogInterface.OnClickListener onClickListener, boolean force) {
        AlertDialog.Builder builder = getDialog(context);
        builder.setMessage(Html.fromHtml(message));
        builder.setPositiveButton("确定", onClickListener);
        if (!force) {
            builder.setNegativeButton("取消", null);
        }
        return builder;
    }

    public static AlertDialog.Builder getConfirmDialog(Context context, String message,
                                                       DialogInterface.OnClickListener onOkClickListener,
                                                       DialogInterface.OnClickListener onCancleClickListener) {
        AlertDialog.Builder builder = getDialog(context);
        builder.setMessage(message);
        builder.setPositiveButton("确定", onOkClickListener);
        builder.setNegativeButton("取消", onCancleClickListener);
        return builder;
    }

}
